package com.dt.platform.oa.controller;

import java.util.*;
import com.dt.platform.domain.oa.ProjectPhase;
import com.dt.platform.domain.oa.meta.ProjectPhaseMeta;
import com.dt.platform.proxy.oa.ProjectServiceProxy;
import com.github.foxnic.commons.lang.StringUtil;
import com.github.foxnic.dao.data.Rcd;
import com.github.foxnic.dao.data.RcdSet;
import com.github.foxnic.sql.expr.ConditionExpr;
import org.github.foxnic.web.domain.hrm.Person;
import org.github.foxnic.web.framework.web.SuperController;
import org.github.foxnic.web.session.SessionUser;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import com.github.foxnic.commons.collection.CollectorUtil;
import com.github.foxnic.dao.entity.ReferCause;
import com.github.foxnic.api.swagger.InDoc;
import org.github.foxnic.web.framework.sentinel.SentinelExceptionUtil;
import com.github.foxnic.api.swagger.ApiParamSupport;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.dt.platform.proxy.oa.ProjectTaskServiceProxy;
import com.dt.platform.domain.oa.meta.ProjectTaskVOMeta;
import com.dt.platform.domain.oa.ProjectTask;
import com.dt.platform.domain.oa.ProjectTaskVO;
import com.github.foxnic.api.transter.Result;
import com.github.foxnic.dao.data.SaveMode;
import com.github.foxnic.dao.excel.ExcelWriter;
import com.github.foxnic.springboot.web.DownloadUtil;
import com.github.foxnic.dao.data.PagedList;
import java.util.Date;
import java.sql.Timestamp;
import com.github.foxnic.api.error.ErrorDesc;
import com.github.foxnic.commons.io.StreamUtil;
import java.util.Map;
import com.github.foxnic.dao.excel.ValidateResult;
import java.io.InputStream;
import com.dt.platform.domain.oa.meta.ProjectTaskMeta;
import java.math.BigDecimal;
import org.github.foxnic.web.domain.system.DictItem;
import org.github.foxnic.web.domain.hrm.Employee;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiImplicitParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.dt.platform.oa.service.IProjectTaskService;
import com.github.foxnic.api.validate.annotations.NotNull;

/**
 * <p>
 * 项目任务接口控制器
 * </p>
 * @author 金杰 , maillank@qq.com
 * @since 2025-06-19 21:01:41
 */
@InDoc
@Api(tags = "项目任务")
@RestController("OaProjectTaskController")
public class ProjectTaskController extends SuperController {

    @Autowired
    private IProjectTaskService projectTaskService;

    /**
     * 添加项目任务
     */
    @ApiOperation(value = "添加项目任务")
    @ApiImplicitParams({
		@ApiImplicitParam(name = ProjectTaskVOMeta.ACTUAL_E_TIME, value = "实际完成日期", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ACTUAL_W_TIME, value = "实际工时", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.CODE, value = "任务编号", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.CT, value = "详细描述", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ESTIMATE_E_TIME, value = "预计结束日期", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ESTIMATE_W_TIME, value = "预估工时", required = false, dataTypeClass = BigDecimal.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.FILE_ID, value = "附件", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.LEADER_ID, value = "负责人员", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.NAME, value = "任务主题", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.PRIORITY, value = "优先级", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.PROJECT_ID, value = "项目名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.STATUS, value = "状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.TASK_STATUS, value = "任务状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.WORK_TYPE, value = "工作类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.FINISH_PCT, value = "完成进度", required = false, dataTypeClass = BigDecimal.class)
	})
    @ApiParamSupport(ignoreDBTreatyProperties = true, ignoreDefaultVoProperties = true, ignorePrimaryKey = true)
    @ApiOperationSupport(order = 1, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = ProjectTaskServiceProxy.INSERT, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectTaskServiceProxy.INSERT)
    public Result insert(ProjectTaskVO projectTaskVO) {
        Result result = projectTaskService.insert(projectTaskVO, false);
        return result;
    }

    /**
     * 删除项目任务
     */
    @ApiOperation(value = "删除项目任务")
    @ApiImplicitParams({
		@ApiImplicitParam(name = ProjectTaskVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class)
	})
    @ApiOperationSupport(order = 2, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = ProjectTaskServiceProxy.DELETE, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectTaskServiceProxy.DELETE)
    public Result deleteById(String id) {
        this.validator().asserts(id).require("缺少id值");
        if (this.validator().failure()) {
            return this.validator().getFirstResult();
        }
        // 引用校验
        ReferCause cause = projectTaskService.hasRefers(id);
        // 判断是否可以删除
        this.validator().asserts(cause.hasRefer()).requireEqual("不允许删除当前记录：" + cause.message(), false);
        if (this.validator().failure()) {
            return this.validator().getFirstResult().messageLevel4Confirm();
        }
        Result result = projectTaskService.deleteByIdLogical(id);
        return result;
    }

    /**
     * 批量删除项目任务 <br>
     * 联合主键时，请自行调整实现
     */
    @ApiOperation(value = "批量删除项目任务")
    @ApiImplicitParams({
		@ApiImplicitParam(name = ProjectTaskVOMeta.IDS, value = "主键清单", required = true, dataTypeClass = List.class, example = "[1,3,4]")
	})
    @ApiOperationSupport(order = 3, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = ProjectTaskServiceProxy.DELETE_BY_IDS, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectTaskServiceProxy.DELETE_BY_IDS)
    public Result deleteByIds(List<String> ids) {
        // 参数校验
        this.validator().asserts(ids).require("缺少ids参数");
        if (this.validator().failure()) {
            return this.validator().getFirstResult();
        }
        // 查询引用
        Map<String, ReferCause> causeMap = projectTaskService.hasRefers(ids);
        // 收集可以删除的ID值
        List<String> canDeleteIds = new ArrayList<>();
        for (Map.Entry<String, ReferCause> e : causeMap.entrySet()) {
            if (!e.getValue().hasRefer()) {
                canDeleteIds.add(e.getKey());
            }
        }
        // 执行删除
        if (canDeleteIds.isEmpty()) {
            // 如果没有一行可以被删除
            return ErrorDesc.failure().message("无法删除您选中的数据行：").data(0).addErrors(CollectorUtil.collectArray(CollectorUtil.filter(causeMap.values(), (e) -> {
                return e.hasRefer();
            }), ReferCause::message, String.class)).messageLevel4Confirm();
        } else if (canDeleteIds.size() == ids.size()) {
            // 如果全部可以删除
            Result result = projectTaskService.deleteByIdsLogical(canDeleteIds);
            return result;
        } else if (canDeleteIds.size() > 0 && canDeleteIds.size() < ids.size()) {
            // 如果部分行可以删除
            Result result = projectTaskService.deleteByIdsLogical(canDeleteIds);
            if (result.failure()) {
                return result;
            } else {
                return ErrorDesc.success().message("已删除 " + canDeleteIds.size() + " 行，但另有 " + (ids.size() - canDeleteIds.size()) + " 行数据无法删除").data(canDeleteIds.size()).addErrors(CollectorUtil.collectArray(CollectorUtil.filter(causeMap.values(), (e) -> {
                    return e.hasRefer();
                }), ReferCause::message, String.class)).messageLevel4Confirm();
            }
        } else {
            // 理论上，这个分支不存在
            return ErrorDesc.success().message("数据删除未处理");
        }
    }

    /**
     * 更新项目任务
     */
    @ApiOperation(value = "更新项目任务")
    @ApiImplicitParams({
		@ApiImplicitParam(name = ProjectTaskVOMeta.ACTUAL_E_TIME, value = "实际完成日期", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ACTUAL_W_TIME, value = "实际工时", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.CODE, value = "任务编号", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.CT, value = "详细描述", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ESTIMATE_E_TIME, value = "预计结束日期", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ESTIMATE_W_TIME, value = "预估工时", required = false, dataTypeClass = BigDecimal.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.FILE_ID, value = "附件", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.LEADER_ID, value = "负责人员", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.NAME, value = "任务主题", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.PRIORITY, value = "优先级", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.PROJECT_ID, value = "项目名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.STATUS, value = "状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.TASK_STATUS, value = "任务状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.WORK_TYPE, value = "工作类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.FINISH_PCT, value = "完成进度", required = false, dataTypeClass = BigDecimal.class)
	})
    @ApiParamSupport(ignoreDBTreatyProperties = true, ignoreDefaultVoProperties = true)
    @ApiOperationSupport(order = 4, author = "金杰 , maillank@qq.com", ignoreParameters = { ProjectTaskVOMeta.PAGE_INDEX, ProjectTaskVOMeta.PAGE_SIZE, ProjectTaskVOMeta.SEARCH_FIELD, ProjectTaskVOMeta.FUZZY_FIELD, ProjectTaskVOMeta.SEARCH_VALUE, ProjectTaskVOMeta.DIRTY_FIELDS, ProjectTaskVOMeta.SORT_FIELD, ProjectTaskVOMeta.SORT_TYPE, ProjectTaskVOMeta.DATA_ORIGIN, ProjectTaskVOMeta.QUERY_LOGIC, ProjectTaskVOMeta.REQUEST_ACTION, ProjectTaskVOMeta.IDS })
    @SentinelResource(value = ProjectTaskServiceProxy.UPDATE, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectTaskServiceProxy.UPDATE)
    public Result update(ProjectTaskVO projectTaskVO) {
        Result result = projectTaskService.update(projectTaskVO, SaveMode.DIRTY_OR_NOT_NULL_FIELDS, false);
        return result;
    }

    /**
     * 保存项目任务
     */
    @ApiOperation(value = "保存项目任务")
    @ApiImplicitParams({
		@ApiImplicitParam(name = ProjectTaskVOMeta.ACTUAL_E_TIME, value = "实际完成日期", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ACTUAL_W_TIME, value = "实际工时", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.CODE, value = "任务编号", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.CT, value = "详细描述", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ESTIMATE_E_TIME, value = "预计结束日期", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ESTIMATE_W_TIME, value = "预估工时", required = false, dataTypeClass = BigDecimal.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.FILE_ID, value = "附件", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.LEADER_ID, value = "负责人员", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.NAME, value = "任务主题", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.PRIORITY, value = "优先级", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.PROJECT_ID, value = "项目名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.STATUS, value = "状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.TASK_STATUS, value = "任务状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.WORK_TYPE, value = "工作类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.FINISH_PCT, value = "完成进度", required = false, dataTypeClass = BigDecimal.class)
	})
    @ApiParamSupport(ignoreDBTreatyProperties = true, ignoreDefaultVoProperties = true)
    @ApiOperationSupport(order = 5, ignoreParameters = { ProjectTaskVOMeta.PAGE_INDEX, ProjectTaskVOMeta.PAGE_SIZE, ProjectTaskVOMeta.SEARCH_FIELD, ProjectTaskVOMeta.FUZZY_FIELD, ProjectTaskVOMeta.SEARCH_VALUE, ProjectTaskVOMeta.DIRTY_FIELDS, ProjectTaskVOMeta.SORT_FIELD, ProjectTaskVOMeta.SORT_TYPE, ProjectTaskVOMeta.DATA_ORIGIN, ProjectTaskVOMeta.QUERY_LOGIC, ProjectTaskVOMeta.REQUEST_ACTION, ProjectTaskVOMeta.IDS })
    @SentinelResource(value = ProjectTaskServiceProxy.SAVE, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectTaskServiceProxy.SAVE)
    public Result save(ProjectTaskVO projectTaskVO) {
        Result result = projectTaskService.save(projectTaskVO, SaveMode.DIRTY_OR_NOT_NULL_FIELDS, false);
        return result;
    }

    /**
     * 获取项目任务
     */
    @ApiOperation(value = "获取项目任务")
    @ApiImplicitParams({
		@ApiImplicitParam(name = ProjectTaskVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class, example = "1")
	})
    @ApiOperationSupport(order = 6, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = ProjectTaskServiceProxy.GET_BY_ID, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectTaskServiceProxy.GET_BY_ID)
    public Result<ProjectTask> getById(String id) {
        Result<ProjectTask> result = new Result<>();
        ProjectTask projectTask = projectTaskService.getById(id);
        // join 关联的对象
        projectTaskService.dao().fill(projectTask).with(ProjectTaskMeta.PRIORITY_DICT).with(ProjectTaskMeta.PROJECT).with(ProjectTaskMeta.LEADER).with(ProjectTaskMeta.WORK_TYPE_DICT).with(ProjectTaskMeta.PART_USER_LIST).execute();
        projectTaskService.dao().join(projectTask.getLeader(), Person.class);
        List<Employee> list = projectTask.getPartUserList();
        projectTaskService.dao().join(list, Person.class);

        String sql="select task_id id,sum(work_time) work_time from oa_project_task_rcd a,oa_project_task b \n" +
                "where b.id=? and a.task_id=b.id and a.deleted=0 and b.deleted=0 \n" +
                "group by task_id";
        RcdSet rs=projectTaskService.dao().query(sql,id);
        Map<Object, Rcd> map=rs.getMappedRcds("id");
        if (map.containsKey(id)) {
            Rcd rcd = map.get(id);
            projectTask.setWorkTimeTotal(rcd.getString("work_time"));
        }else{
            projectTask.setWorkTimeTotal("0");
        }
        result.success(true).data(projectTask);
        return result;
    }

    /**
     * 批量获取项目任务 <br>
     * 联合主键时，请自行调整实现
     */
    @ApiOperation(value = "批量获取项目任务")
    @ApiImplicitParams({
		@ApiImplicitParam(name = ProjectTaskVOMeta.IDS, value = "主键清单", required = true, dataTypeClass = List.class, example = "[1,3,4]")
	})
    @ApiOperationSupport(order = 3, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = ProjectTaskServiceProxy.GET_BY_IDS, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectTaskServiceProxy.GET_BY_IDS)
    public Result<List<ProjectTask>> getByIds(List<String> ids) {
        Result<List<ProjectTask>> result = new Result<>();
        List<ProjectTask> list = projectTaskService.queryListByIds(ids);
        result.success(true).data(list);
        return result;
    }

    /**
     * 查询项目任务
     */
    @ApiOperation(value = "查询项目任务")
    @ApiImplicitParams({
		@ApiImplicitParam(name = ProjectTaskVOMeta.ACTUAL_E_TIME, value = "实际完成日期", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ACTUAL_W_TIME, value = "实际工时", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.CODE, value = "任务编号", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.CT, value = "详细描述", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ESTIMATE_E_TIME, value = "预计结束日期", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ESTIMATE_W_TIME, value = "预估工时", required = false, dataTypeClass = BigDecimal.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.FILE_ID, value = "附件", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.LEADER_ID, value = "负责人员", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.NAME, value = "任务主题", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.PRIORITY, value = "优先级", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.PROJECT_ID, value = "项目名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.STATUS, value = "状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.TASK_STATUS, value = "任务状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.WORK_TYPE, value = "工作类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.FINISH_PCT, value = "完成进度", required = false, dataTypeClass = BigDecimal.class)
	})
    @ApiOperationSupport(order = 5, author = "金杰 , maillank@qq.com", ignoreParameters = { ProjectTaskVOMeta.PAGE_INDEX, ProjectTaskVOMeta.PAGE_SIZE })
    @SentinelResource(value = ProjectTaskServiceProxy.QUERY_LIST, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectTaskServiceProxy.QUERY_LIST)
    public Result<List<ProjectTask>> queryList(ProjectTaskVO sample) {
        Result<List<ProjectTask>> result = new Result<>();
        List<ProjectTask> list = projectTaskService.queryList(sample);
        result.success(true).data(list);
        return result;
    }

    /**
     */
    @ApiOperation(value = "动作")
    @ApiOperationSupport(order = 2, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = ProjectTaskServiceProxy.ACTION, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectTaskServiceProxy.ACTION)
    public Result action(String id, String action) {
        Result result = projectTaskService.action(id, action);
        return result;
    }

    /**
     * 分页查询项目任务
     */
    @ApiOperation(value = "分页查询项目任务")
    @ApiImplicitParams({
		@ApiImplicitParam(name = ProjectTaskVOMeta.ACTUAL_E_TIME, value = "实际完成日期", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ACTUAL_W_TIME, value = "实际工时", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.CODE, value = "任务编号", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.CT, value = "详细描述", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ESTIMATE_E_TIME, value = "预计结束日期", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ESTIMATE_W_TIME, value = "预估工时", required = false, dataTypeClass = BigDecimal.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.FILE_ID, value = "附件", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.LEADER_ID, value = "负责人员", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.NAME, value = "任务主题", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.PRIORITY, value = "优先级", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.PROJECT_ID, value = "项目名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.STATUS, value = "状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.TASK_STATUS, value = "任务状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.WORK_TYPE, value = "工作类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectTaskVOMeta.FINISH_PCT, value = "完成进度", required = false, dataTypeClass = BigDecimal.class)
	})
    @ApiOperationSupport(order = 8, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = ProjectTaskServiceProxy.QUERY_PAGED_LIST, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectTaskServiceProxy.QUERY_PAGED_LIST)
    public Result<PagedList<ProjectTask>> queryPagedList(ProjectTaskVO sample) {
        Result<PagedList<ProjectTask>> result = new Result<>();
        ConditionExpr expr=new ConditionExpr();
        expr.and("1=1");
        if(!StringUtil.isBlank(sample.getTabStatus())){
            if("acting".equals(sample.getTabStatus())){
                expr.and("task_status in ('wait','acting')");
            }else if("finish".equals(sample.getTabStatus())){
                expr.and("task_status in ('finish','close','cancel')");
            }
        }
        if(!StringUtil.isBlank(sample.getSelectRange())){
            if("mine".equals(sample.getSelectRange())){
                String cUserId= SessionUser.getCurrent().getActivatedEmployeeId();
                //只有项目中有指定，就可见
                String ownerSql="select distinct owner_source task_id from (\n" +
                        "select 'project' range_type,leader_id user_id,'leader' join_type, id owner_source,id project_id from oa_project where deleted=0  and leader_id is not null and leader_id<>''\n" +
                        "union all\n" +
                        "select 'project' range_type,user_id,'member' join_type, id owner_source,project_id project_id from oa_project_user where     deleted=0 and status='in' and own_type='project' and user_id is not null and user_id<>''\n" +
                        "union all\n" +
                        "select 'task' range_type,leader_id,'leader' join_type,id owner_source,project_id project_id from oa_project_task where deleted=0  and leader_id is not null and leader_id<>''\n" +
                        "union all\n" +
                        "select 'task' range_type,b.user_id,'member' join_type,a.id owner_source,a.project_id project_id from oa_project_task a,oa_project_user b where a.id=b.project_id and a.deleted=0   and b.user_id is not null and b.user_id<>''\n" +
                        "union all\n" +
                        "select 'phase' range_type,leader_id,'leader' join_type,id owner_source ,project_id project_id from oa_project_phase where deleted=0   and leader_id is not null and leader_id<>''\n" +
                        "union all\n" +
                        "select 'phase' range_type,b.user_id,'member' join_type,a.id owner_source,a.project_id project_id from oa_project_phase a,oa_project_user b where a.id=b.project_id and a.deleted=0   and b.user_id is not null and b.user_id<>''\n" +
                        ") tab where range_type='task' and user_id=?";
                expr.and("id in ("+ownerSql+")",cUserId);
            }
        }
        PagedList<ProjectTask> list = projectTaskService.queryPagedList(sample,expr, sample.getPageSize(), sample.getPageIndex());
        // join 关联的对象
        projectTaskService.dao().fill(list).with(ProjectTaskMeta.PRIORITY_DICT).with(ProjectTaskMeta.PROJECT).with(ProjectTaskMeta.LEADER).with(ProjectTaskMeta.WORK_TYPE_DICT).with(ProjectTaskMeta.PART_USER_LIST).execute();
        List<Employee> leaders = CollectorUtil.collectList(list, ProjectTask::getLeader);
        projectTaskService.dao().join(leaders, Person.class);
        List<List<Employee>> userList = CollectorUtil.collectList(list, ProjectTask::getPartUserList);
        List<Employee> member = userList.stream().collect(ArrayList::new, ArrayList::addAll, ArrayList::addAll);
        projectTaskService.dao().join(member, Person.class);
        String sql="select task_id id,sum(work_time) work_time from oa_project_task_rcd a,oa_project_task b \n" +
                "where a.task_id=b.id and a.deleted=0 and b.deleted=0 \n" +
                "group by task_id";
        RcdSet rs=projectTaskService.dao().query(sql);
        Map<Object, Rcd> map=rs.getMappedRcds("id");
        for(int i=0;i<list.getList().size();i++) {
            String id = list.getList().get(i).getId();
            if (map.containsKey(id)) {
                Rcd rcd = map.get(id);
                list.getList().get(i).setWorkTimeTotal(rcd.getString("work_time"));
            }else{
                list.getList().get(i).setWorkTimeTotal("0");
            }
        }
        result.success(true).data(list);
        return result;
    }
}
